package com.zhsj.OkeHomeConsumption.ui.activity.citypartner;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.TitleBar;
import com.hjq.base.BaseDialog;
import com.hjq.dialog.InputDialog;
import com.hjq.dialog.MenuDialog;
import com.hjq.widget.SettingBar;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.adapter.GridViewAddImgesAdpter;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.CityPartnerShopBean;
import com.zhsj.OkeHomeConsumption.empty.MerchantTypeBean;
import com.zhsj.OkeHomeConsumption.empty.SelectCityEnity;
import com.zhsj.OkeHomeConsumption.empty.StoredetailsTypeBean;
import com.zhsj.OkeHomeConsumption.empty.UpLoadImgBean;
import com.zhsj.OkeHomeConsumption.helper.GlideEngine;
import com.zhsj.OkeHomeConsumption.ui.activity.MapActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.StoredetailsPresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.citypartner.mvp.StoredetailsUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.ClearEditText;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.SelectCityPicker;
import com.zhsj.OkeHomeConsumption.utils.UpLoadGridView;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *商家招募--商家维护--查看详情
 *
 * **/

public class StoredetailsActivity extends MyActivity implements StoredetailsUIInterFace {
    //待沟通商家
    @BindView(R.id.sb_mine_data_wallet)
    SettingBar sbMineDataWallet;

    //基本信息
    @BindView(R.id.sb_mine_data_margin)
    SettingBar sbMineDataMargin;

    //店铺名称
    @BindView(R.id.sb_citypartner_store_name)
    SettingBar sbCitypartnerStoreName;

    //商家电话
    @BindView(R.id.sb_citypartner_store_phone)
    SettingBar sbCitypartnerStorePhone;

    //负责人
    @BindView(R.id.sb_citypartner_store_principal)
    SettingBar sbCitypartnerStorePrincipal;

    //所在城市
    @BindView(R.id.sb_citypartner_store_city)
    SettingBar sbCitypartnerStoreCity;

    //店铺地址
    @BindView(R.id.sb_citypartner_store_address)
    SettingBar sbCitypartnerStoreAddress;

    //经营类别
    @BindView(R.id.sb_citypartner_store_type)
    SettingBar sbCitypartnerStoreType;

    //主营业务
    @BindView(R.id.sb_citypartner_store_business)
    SettingBar sbCitypartnerStoreBusiness;

    //主营产品
    @BindView(R.id.sb_citypartner_store_product)
    SettingBar sbCitypartnerStoreProduct;

    //店铺位置
    @BindView(R.id.sb_citypartner_store_map)
    SettingBar sbCitypartnerStoreMap;
    private List<StoredetailsTypeBean> shopScopeList;//存储选中的值

    //开始营业时间
    @BindView(R.id.sb_starttimer_store_product)
    SettingBar sb_starttimer_store_product;

    //结束营业时间
    @BindView(R.id.sb_endtimer_store_product)
    SettingBar sb_endtimer_store_product;

    //门脸图
    @BindView(R.id.menliantu)
    UpLoadGridView menliantu;
    private GridViewAddImgesAdpter menliantu_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> menlian_imageServiceList;

    //提交
    @BindView(R.id.btn_test_login)
    Button btnTestLogin;

    //店铺logo
    @BindView(R.id.logogradle)
    UpLoadGridView logogradle;
    private GridViewAddImgesAdpter logogradle_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> logogradle_imageServiceList;

    //店铺背景
    @BindView(R.id.dianpubeijingtu)
    UpLoadGridView dianpubeijingtu;
    private GridViewAddImgesAdpter dianpubeijingtu_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> dianpubeijingtu_imageServiceList;

    //店内图
    @BindView(R.id.dianneihuanjing)
    UpLoadGridView dianneihuanjing;
    private GridViewAddImgesAdpter diannei_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> diannei_imageServiceList;


    //营业执照
    @BindView(R.id.jingyingzhizhao)
    UpLoadGridView jingyingzhizhao;
    private GridViewAddImgesAdpter yingye_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> yingye_imageServiceList;


    //许可证
    @BindView(R.id.xukezheng)
    UpLoadGridView xukezheng;
    private GridViewAddImgesAdpter xukezheng_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> xukezheng_imageServiceList;


    //店铺图
    @BindView(R.id.dianputu)
    UpLoadGridView dianputu;
    private GridViewAddImgesAdpter dianputu_gridViewAddImgesAdpter;
    private List<UpLoadImgBean> dianputu_imageServiceList;

    //平台管理费
    @BindView(R.id.guanlifei_text)
    ClearEditText guanlifeiText;

    //保证金
    @BindView(R.id.baozhengjin_text)
    ClearEditText baozhengjinText;


    @BindView(R.id.tb_test_a_bar)
    TitleBar tb_test_a_bar;


    //门脸图 1  店内图2   营业执照3  许可证4 log5 背景6 店铺图7
    private int REQUEST_CODE_CHOOSE = -1;

    // 图片集合
    private List<File> datas_menlian = null;
    private List<UpLoadImgBean> imageServiceList = null;

    private StoredetailsPresenter minePersonPresenter;

    private ArrayList<MerchantTypeBean> mMerchantTypeLists = null;
    private ArrayList<MerchantTypeBean> mMerchantChildTypeLists = null;

    private ArrayList<SelectCityEnity> mSelectCityLists = null;

    private ArrayList<String> options1Items = new ArrayList<>();//省
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//区
    private int proviceId = -1;//省
    private int mcityId = -1;//市
    private int districCountryId = -1;//区
    private int streetId;//街道

    private int depositLow = -1;//保证金范围
    private int depositHigh = -1;//保证金范围

    private int manageRateLow = -1;//凭平台管理费范围
    private int manageRateHigh = -1;//凭平台管理费范围

    //经营类别id （用于获取主营业务）
    private int TypeId = -1;

    //主营业务id
    private int typeid = -1;

    private CityPartnerShopBean mCityPartnerShopbean;

    private String flag;

    private String latitude = "";//纬度

    private String longitude = "";//经度

    private String shopLocation = "";//店铺定位

    @Override
    protected int getLayoutId() {
        return R.layout.activity_storedatails;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_test_a_bar;
    }

    @Override
    protected void initView() {
        minePersonPresenter = new StoredetailsPresenter(getActivity(), this);
        Bundle bundle = getIntent().getExtras();
        flag = getIntent().getStringExtra("flag");
        if (flag.equals("1")) {
            tb_test_a_bar.setTitle("查看详情");
            btnTestLogin.setVisibility(View.GONE);
        } else {
            tb_test_a_bar.setTitle("完善信息");
        }
        mCityPartnerShopbean = (CityPartnerShopBean) bundle.getSerializable("CityPartnerShop");
        minePersonPresenter.FindByUserIdGet(mCityPartnerShopbean.getUserId());
        datas_menlian = new ArrayList<>();
        imageServiceList = new ArrayList<>();
        diannei_imageServiceList = new ArrayList<>();
        dianputu_imageServiceList = new ArrayList<>();
        menlian_imageServiceList = new ArrayList<>();
        yingye_imageServiceList = new ArrayList<>();
        xukezheng_imageServiceList = new ArrayList<>();
        logogradle_imageServiceList = new ArrayList<>();
        dianpubeijingtu_imageServiceList = new ArrayList<>();
        shopScopeList = new ArrayList<>();
    }

    @Override
    protected void initData() {
        menliantu_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(menlian_imageServiceList, getActivity());
        menliantu_gridViewAddImgesAdpter.setMaxImages(2);
        menliantu.setAdapter(menliantu_gridViewAddImgesAdpter);
        menliantu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //门脸图
                REQUEST_CODE_CHOOSE = 1;


                ShootingDialog(REQUEST_CODE_CHOOSE, 1);
            }
        });

        diannei_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(diannei_imageServiceList, getActivity());
        diannei_gridViewAddImgesAdpter.setMaxImages(8);
        dianneihuanjing.setAdapter(diannei_gridViewAddImgesAdpter);
        dianneihuanjing.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //店面图
                REQUEST_CODE_CHOOSE = 2;
                ShootingDialog(REQUEST_CODE_CHOOSE, 8);
            }
        });

        yingye_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(yingye_imageServiceList, getActivity());
        yingye_gridViewAddImgesAdpter.setMaxImages(2);
        jingyingzhizhao.setAdapter(yingye_gridViewAddImgesAdpter);
        jingyingzhizhao.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //营业执照
                REQUEST_CODE_CHOOSE = 3;
                ShootingDialog(REQUEST_CODE_CHOOSE, 1);
            }
        });

        xukezheng_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(xukezheng_imageServiceList, getActivity());
        xukezheng_gridViewAddImgesAdpter.setMaxImages(2);
        xukezheng.setAdapter(xukezheng_gridViewAddImgesAdpter);
        xukezheng.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //许可证
                REQUEST_CODE_CHOOSE = 4;
                ShootingDialog(REQUEST_CODE_CHOOSE, 1);
            }
        });

        logogradle_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(logogradle_imageServiceList, getActivity());
        logogradle_gridViewAddImgesAdpter.setMaxImages(2);
        logogradle.setAdapter(logogradle_gridViewAddImgesAdpter);
        logogradle.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //店铺logo
                REQUEST_CODE_CHOOSE = 5;
                ShootingDialog(REQUEST_CODE_CHOOSE, 1);
            }
        });

        dianpubeijingtu_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(dianpubeijingtu_imageServiceList, getActivity());
        dianpubeijingtu_gridViewAddImgesAdpter.setMaxImages(2);
        dianpubeijingtu.setAdapter(dianpubeijingtu_gridViewAddImgesAdpter);
        dianpubeijingtu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //店铺背景图
                REQUEST_CODE_CHOOSE = 6;
                ShootingDialog(REQUEST_CODE_CHOOSE, 1);
            }
        });

        dianputu_gridViewAddImgesAdpter = new GridViewAddImgesAdpter(dianputu_imageServiceList, getActivity());
        dianputu_gridViewAddImgesAdpter.setMaxImages(8);
        dianputu.setAdapter(dianputu_gridViewAddImgesAdpter);
        dianputu.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //店铺图
                REQUEST_CODE_CHOOSE = 7;
                ShootingDialog(REQUEST_CODE_CHOOSE, 8);
            }
        });
    }


    private void submit() {
        String shopname = sbCitypartnerStoreName.getRightText().toString();
        if (shopname.equals("")) {
            toast("请输入商铺名称");
            return;
        }

        String shopphone = sbCitypartnerStorePhone.getRightText().toString();
        if (shopphone.equals("")) {
            toast("请输入商铺电话");
            return;
        }

        String torePrin = sbCitypartnerStorePrincipal.getRightText().toString();
        if (torePrin.equals("")) {
            toast("请输入商铺负责人");
            return;
        }
        String city = sbCitypartnerStoreCity.getRightText().toString();
        if (city.equals("")) {
            toast("请选择城市");
            return;
        }
        String StoreAddress = sbCitypartnerStoreAddress.getRightText().toString();
        if (StoreAddress.equals("")) {
            toast("请填写详细地址");
            return;
        }
        String StoreType = sbCitypartnerStoreType.getRightText().toString();
        if (StoreType.equals("")) {
            toast("请选择经营类别");
            return;
        }
        String StoreBusiness = sbCitypartnerStoreBusiness.getRightText().toString();
        if (StoreBusiness.equals("")) {
            toast("请选择主营业务");
            return;
        }
        String StoreProduct = sbCitypartnerStoreProduct.getRightText().toString();
        List<Integer> listint = new ArrayList<>();
        for (int i = 0; i < shopScopeList.size(); i++) {
            listint.add(shopScopeList.get(i).getId());
        }
        if (StoreProduct.equals("") && listint.size() != 0) {
            toast("请选择主营产品");
            return;
        }

        String starttimer = sb_starttimer_store_product.getRightText().toString();
        if (starttimer.equals("")) {
            toast("请选择开始营业时间");
            return;
        }
        String endtimer = sb_endtimer_store_product.getRightText().toString();
        if (endtimer.equals("")) {
            toast("请选择结束营业时间");
            return;
        }
        if (logogradle_imageServiceList.size() == 0) {
            toast("请上传店铺LOGO");
            return;
        }
        if (dianpubeijingtu_imageServiceList.size() == 0) {
            toast("请上传店铺背景图");
            return;
        }
        if (dianputu_imageServiceList.size() == 0) {
            toast("请上传店铺图");
            return;
        }
        if (menlian_imageServiceList.size() == 0) {
            toast("请上传门脸图");
            return;
        }

        if (diannei_imageServiceList.size() == 0) {
            toast("请上传店内环境图");
            return;
        }
        if (yingye_imageServiceList.size() == 0) {
            toast("请上传营业执照");
            return;
        }
        if (xukezheng_imageServiceList.size() == 0) {
            toast("请上传许可证");
            return;
        }

        String guanlifei = guanlifeiText.getText().toString();
        if (guanlifei.equals("")) {
            toast("请输入平台管理费");
            return;
        }
        double i = Double.parseDouble(guanlifei);
        if (i >= manageRateLow && i <= manageRateHigh) {

        } else {
            toast("请输入正确范围的平台管理费");
            return;
        }

        String baozhengjin = baozhengjinText.getText().toString();
        if (baozhengjin.equals("")) {
            toast("请输入保证金");
            return;
        }
        double q = Double.parseDouble(baozhengjin);
        if (q >= depositLow && q <= depositHigh) {

        } else {
            toast("请输入正确范围的保证金");
            return;
        }
        if (TypeId == -1) {
            toast("请选择经营类别");
            return;
        }
        if (typeid == -1) {
            toast("请选择主营业务");
            return;
        }

        if (latitude.equals("") || longitude.equals("")) {
            toast("请选择店铺位置");
            return;
        }

        Gson gson = new Gson();
        Map<String, Object> mapjson = new HashMap<>();


        mapjson.put("latitude", latitude);
        mapjson.put("longitude", longitude);
        mapjson.put("shopLocation", shopLocation);
        mapjson.put("id", mCityPartnerShopbean.getId());
        mapjson.put("shopName", shopname);//商铺名称
        mapjson.put("shopMobile", shopphone);//商铺电话
        mapjson.put("shopLeader", torePrin);//负责人
        mapjson.put("cityName", city);
        mapjson.put("address", StoreAddress);
        mapjson.put("categoryLevel1Name", TypeId);
        mapjson.put("categoryLevel2Name", typeid);
        mapjson.put("shopBusinessScopeText", listint);
        mapjson.put("workTime", starttimer + "-" + endtimer);

        mapjson.put("shopLogo", slipet(logogradle_imageServiceList.get(0).getPhotoPath().toString()));
        mapjson.put("shopBackImg", slipet(dianpubeijingtu_imageServiceList.get(0).getPhotoPath().toString()));
        mapjson.put("houseFacade", slipet(menlian_imageServiceList.get(0).getPhotoPath().toString()));
        List<String> list1 = new ArrayList<>();
        for (int a = 0; a < dianputu_imageServiceList.size(); a++) {
            list1.add(dianputu_imageServiceList.get(a).getPhotoPath());
        }
        mapjson.put("shopImgs", list1);
        List<String> list2 = new ArrayList<>();
        for (int a = 0; a < diannei_imageServiceList.size(); a++) {
            list2.add(diannei_imageServiceList.get(a).getPhotoPath());
        }
        mapjson.put("shopInsidePicList", list2);
        mapjson.put("businessLicensePic", slipet(yingye_imageServiceList.get(0).getPhotoPath().toString()));
        mapjson.put("managementPic", slipet(xukezheng_imageServiceList.get(0).getPhotoPath().toString()));

        mapjson.put("platformFee", guanlifei);
        mapjson.put("deposit", baozhengjin);

        mapjson.put("provinceId", proviceId);//省id
        mapjson.put("cityId", mcityId);//市ID
        mapjson.put("districCountryId", districCountryId);//县ID
        mapjson.put("streetId", streetId);//街道id

        String lifeCityCopartnerDTO = gson.toJson(mapjson);//map转json字符串
        minePersonPresenter.Update(lifeCityCopartnerDTO);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void Update(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                finish();
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("Update", "店铺维护与详情");
        }
    }

    @Override
    public void FindByUserIdGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsonobj = jsonOBJ.optJSONObject("data");
                CityPartnerShopBean mCityPartnerShop = JsonUtils.object(jsonobj.toString(), CityPartnerShopBean.class);
                sbCitypartnerStoreName.setRightText(mCityPartnerShop.getShopName());
                sbCitypartnerStorePhone.setRightText(mCityPartnerShop.getShopMobile());
                sbCitypartnerStorePrincipal.setRightText(mCityPartnerShop.getShopLeader());
                sbCitypartnerStoreCity.setRightText(mCityPartnerShop.getCityName());
                sbCitypartnerStoreAddress.setRightText(mCityPartnerShop.getAddress());
                latitude =String.valueOf(mCityPartnerShop.getLatitude()) ;
                longitude = String.valueOf(mCityPartnerShop.getLongitude());
                shopLocation = mCityPartnerShop.getShopLocation();
                sbCitypartnerStoreMap.setRightText(shopLocation);
                if (flag.equals("1")) {
                    sbCitypartnerStoreType.setRightText(mCityPartnerShop.getCategoryLevel1Name());
                    sbCitypartnerStoreBusiness.setRightText(mCityPartnerShop.getCategoryLevel2Name());
                }

                if (mCityPartnerShop.getShopBusinessScopeText() != null) {
                    sbCitypartnerStoreProduct.setRightText(mCityPartnerShop.getShopBusinessScopeText().toString());
                }
                baozhengjinText.setText(mCityPartnerShop.getDeposit() + "");

                proviceId = mCityPartnerShop.getProviceId();//省
                mcityId = mCityPartnerShop.getCityId();//市
                districCountryId = mCityPartnerShop.getDistricCountryId();//区
                streetId = mCityPartnerShop.getStreetId();//街道


                String timestr = mCityPartnerShop.getWorkTime();
                if (!timestr.equals("")) {
                    String strttimer = timestr.substring(0, 4);
                    String enttimer = timestr.substring(timestr.indexOf("-"));
                    sb_starttimer_store_product.setRightText(strttimer);
                    sb_endtimer_store_product.setRightText(enttimer.substring(1));
                }

                guanlifeiText.setText(mCityPartnerShop.getPlatformFee() + "");

                if (mCityPartnerShop.getShopLogo() != null) {
                    logogradle_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getShopLogo()));
                }

                if (mCityPartnerShop.getShopBackImg() != null) {
                    dianpubeijingtu_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getShopBackImg()));
                }

                if (mCityPartnerShop.getHouseFacade() != null) {
                    menlian_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getHouseFacade()));
                }

                if (mCityPartnerShop.getShopImgs() != null) {
                    for (int i = 0; i < mCityPartnerShop.getShopImgs().size(); i++) {
                        dianputu_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getShopImgs().get(i)));

                    }
                }

                if (mCityPartnerShop.getShopInsidePicList() != null) {
                    for (int i = 0; i < mCityPartnerShop.getShopInsidePicList().size(); i++) {
                        diannei_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getShopInsidePicList().get(i)));

                    }
                }

                if (mCityPartnerShop.getBusinessLicensePic() != null) {
                    yingye_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getBusinessLicensePic()));
                }


                if (mCityPartnerShop.getManagementPic() != null) {
                    xukezheng_imageServiceList.add(new UpLoadImgBean(mCityPartnerShop.getManagementPic()));
                }
                initData();
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("商家--- 店铺维护");
        }
    }

    /**
     * 图片选择
     */
    private void Pictureselection(int code, int max) {
        if (code == 0) {
            EasyPhotos.createCamera(this)//参数说明：上下文
                    .setFileProviderAuthority("com.zhsj.OkeHomeConsumption.fileprovider")//参数说明：见下方`FileProvider的配置`
                    .start(101);
        } else {
            EasyPhotos.createAlbum(this, false, GlideEngine.getInstance())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setCount(max)//参数说明：最大可选数，默认1
                    .setPuzzleMenu(false)
                    .start(101);
        }


    }


    private String slipet(String list) {

        return StringUtils.strip(list.toString(), "[]");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK) {
            //返回对象集合：如果你需要了解图片的宽、高、大小、用户是否选中原图选项等信息，可以用这个
            ArrayList<Photo> resultPhotos = data.getParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS);
            datas_menlian.clear();
            for (int i = 0; i < resultPhotos.size(); i++) {
                datas_menlian.add(uri2File(resultPhotos.get(i).path));
            }
            minePersonPresenter.UploadPost(datas_menlian);
        } else if (requestCode == 9) {
            //主营产品
            if (data != null) {
                shopScopeList = (ArrayList<StoredetailsTypeBean>) data.getSerializableExtra("list");
                List<String> list = new ArrayList<>();
                for (int i = 0; i < shopScopeList.size(); i++) {
                    list.add(shopScopeList.get(i).getBusinessScopeName());
                }
                sbCitypartnerStoreProduct.setRightText(StringUtils.strip(list.toString(), "[]"));
            }

        }

        if (requestCode == 1) {
            if(resultCode == -1){
                //地图页面返回
                latitude = data.getStringExtra("latitude");//纬度
                longitude = data.getStringExtra("longitude");//经度

                shopLocation = data.getStringExtra("name");
                sbCitypartnerStoreMap.setRightText(shopLocation);
            }else {
                minePersonPresenter.FindByUserIdGet(mCityPartnerShopbean.getUserId());
            }

        }


    }

    /**
     * 多张图片上传
     */
    @Override
    public void UploadPost(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsondata = jsonOBJ.optJSONArray("data");
                if (imageServiceList.size() != 0) {
                    imageServiceList.clear();
                }
                imageServiceList = new Gson().fromJson(jsondata.toString(), new
                        TypeToken<ArrayList<UpLoadImgBean>>() {
                        }.getType());
                if (imageServiceList != null && imageServiceList.size() > 0) {
                    switch (REQUEST_CODE_CHOOSE) {
                        case 1:
                            //门脸数据
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                menlian_imageServiceList.add(imageServiceList.get(i));
                            }
                            menliantu_gridViewAddImgesAdpter.notifyDataSetChanged(menlian_imageServiceList);
                            break;
                        case 2:
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                diannei_imageServiceList.add(imageServiceList.get(i));
                            }
                            diannei_gridViewAddImgesAdpter.notifyDataSetChanged(diannei_imageServiceList);
                            break;
                        case 3:
                            if (yingye_imageServiceList.size() != 0) {
                                yingye_imageServiceList.clear();
                            }
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                yingye_imageServiceList.add(imageServiceList.get(i));
                            }
                            yingye_gridViewAddImgesAdpter.notifyDataSetChanged(yingye_imageServiceList);
                            break;
                        case 4:
                            if (xukezheng_imageServiceList.size() != 0) {
                                xukezheng_imageServiceList.clear();
                            }
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                xukezheng_imageServiceList.add(imageServiceList.get(i));
                            }
                            xukezheng_gridViewAddImgesAdpter.notifyDataSetChanged(xukezheng_imageServiceList);
                            break;
                        case 5:
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                logogradle_imageServiceList.add(imageServiceList.get(i));
                            }
                            logogradle_gridViewAddImgesAdpter.notifyDataSetChanged(logogradle_imageServiceList);
                            break;
                        case 6:
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                dianpubeijingtu_imageServiceList.add(imageServiceList.get(i));
                            }
                            dianpubeijingtu_gridViewAddImgesAdpter.notifyDataSetChanged(dianpubeijingtu_imageServiceList);
                            break;
                        case 7:
                            for (int i = 0; i < imageServiceList.size(); i++) {
                                dianputu_imageServiceList.add(imageServiceList.get(i));
                            }
                            dianputu_gridViewAddImgesAdpter.notifyDataSetChanged(dianputu_imageServiceList);
                            break;
                    }
                    imageServiceList.clear();

                }
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("城市合伙人--- 多张图片上传");
        }
    }

    @Override
    public void MerchantTypeGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                mMerchantTypeLists = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<MerchantTypeBean>>() {
                        }.getType());
                List<String> datalist = new ArrayList<>();
                for (int i = 0; i < mMerchantTypeLists.size(); i++) {
                    datalist.add(mMerchantTypeLists.get(i).getName());
                }
                MenuDialog(datalist, 1);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("MerchantTypeGet", "获取经营类型异常");
        }
    }

    /*
     * 主营业务
     * */
    @Override
    public void MerchantChildTypeGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                mMerchantChildTypeLists = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<MerchantTypeBean>>() {
                        }.getType());

                List<String> datalist = new ArrayList<>();
                for (int i = 0; i < mMerchantChildTypeLists.size(); i++) {
                    datalist.add(mMerchantChildTypeLists.get(i).getName());
                }
                MenuDialog(datalist, 2);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("MerchantChildTypeGet", "主营业务");
        }
    }

    @Override
    public void CityListPsot(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONArray jsonarr = jsonOBJ.optJSONArray("data");
                // 数据对接正常
                mSelectCityLists = new Gson().fromJson(jsonarr.toString(), new
                        TypeToken<ArrayList<SelectCityEnity>>() {
                        }.getType());
                getSelectCity(mSelectCityLists);

            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("CityListPsot", "获取城市列表");
        }
    }

    /**
     * user转换为file文件
     * 返回值为file类型
     *
     * @param uri
     * @return
     */
    private File uri2File(String uri) {
        File file = new File(uri);
        return file;
    }

    @OnClick({R.id.btn_test_login, R.id.sb_citypartner_store_map, R.id.sb_mine_data_wallet, R.id.sb_mine_data_margin, R.id.sb_citypartner_store_name, R.id.sb_citypartner_store_phone, R.id.sb_citypartner_store_principal, R.id.sb_citypartner_store_city, R.id.sb_citypartner_store_address, R.id.sb_citypartner_store_type, R.id.sb_citypartner_store_business, R.id.sb_citypartner_store_product, R.id.sb_starttimer_store_product, R.id.sb_endtimer_store_product, R.id.guanlifei_text, R.id.baozhengjin_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sb_mine_data_wallet:
                //商户状态
                break;
            case R.id.sb_mine_data_margin:
                //基本信息
                break;
            case R.id.sb_citypartner_store_name:
                //商铺名称
                InputDialog(1, "商铺名称");
                break;
            case R.id.sb_citypartner_store_map:
                //店铺位置
                startActivityForResult(new Intent(this, MapActivity.class).putExtra("latitude",latitude)
                        .putExtra("longitude",longitude).putExtra("shopLocation",shopLocation), 1);
                break;
            case R.id.sb_citypartner_store_phone:
                //商家电话
                InputDialog(2, "商家电话");
                break;
            case R.id.sb_citypartner_store_principal:
                //负责人
                InputDialog(3, "负责人");
                break;
            case R.id.sb_citypartner_store_city:
                //所在城市
                minePersonPresenter.CityListPsot();
                break;
            case R.id.sb_citypartner_store_address:
                //店铺地址
                InputDialog(4, "店铺地址");
                break;
            case R.id.sb_citypartner_store_type:
                //经营类别
                minePersonPresenter.MerchantTypeGet();
                break;
            case R.id.sb_citypartner_store_business:
                //主营业务
                if (TypeId == -1) {
                    toast("请选择主营业务");
                    return;
                }
                minePersonPresenter.MerchantChildTypeGet(TypeId);
                break;
            case R.id.sb_citypartner_store_product:
                //主营产品
                startActivityForResult(new Intent(getActivity(), StoredetailsTypeActivity.class), 9);
                break;
            case R.id.sb_starttimer_store_product:
                //开始营业时间
                checktimer(1);
                break;
            case R.id.sb_endtimer_store_product:
                //结束营业时间
                checktimer(2);
                break;
            case R.id.guanlifei_text:
                //平台管理费
                if (manageRateLow == -1 || manageRateLow == -1) {
                    toast("请选择主营业务");
                    return;
                }
                break;
            case R.id.baozhengjin_text:
                if (depositLow == -1 || depositHigh == -1) {
                    toast("请选择主营业务");
                    return;
                }
                break;
            case R.id.btn_test_login:
                //提交
                submit();
                break;
        }
    }

    private void InputDialog(final int tag, final String title) {
        new InputDialog.Builder(this)
                .setTitle(title)
                .setCancelable(false)
                .setListener(new InputDialog.OnListener() {

                    @Override
                    public void onConfirm(Dialog dialog, String content) {
                        switch (tag) {
                            case 1:
                                if (!sbCitypartnerStoreName.getRightText().equals(content)) {
                                    sbCitypartnerStoreName.setRightText(content);
                                }
                                break;
                            case 2:
                                if (!sbCitypartnerStorePhone.getRightText().equals(content)) {
                                    sbCitypartnerStorePhone.setRightText(content);
                                }
                                break;
                            case 3:
                                if (!sbCitypartnerStorePrincipal.getRightText().equals(content)) {
                                    sbCitypartnerStorePrincipal.setRightText(content);
                                }
                                break;
                            case 4:
                                if (!sbCitypartnerStoreAddress.getRightText().equals(content)) {
                                    sbCitypartnerStoreAddress.setRightText(content);
                                }
                                break;
                        }

                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                    }
                })
                .show();
    }


    private void ShootingDialog(final int code, final int max) {
        String[] arreaArr = getActivity().getResources().getStringArray(R.array.shooting_type);
        final List<String> shooting_type_list = Arrays.asList(arreaArr);
        new MenuDialog.Builder(this)
                .setCancel("取消") // 设置 null 表示不显示取消按钮
                //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
                .setList(shooting_type_list)
                .setListener(new MenuDialog.OnListener() {

                    @Override
                    public void onSelected(Dialog dialog, int position, String text) {
                        switch (position) {
                            case 0:
                                Pictureselection(position, max);
                                break;
                            case 1:
                                Pictureselection(position, max);
                                break;
                        }
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        toast("取消了");
                    }
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
                .show();
    }

    private void MenuDialog(List<String> datalist, final int flage) {
        new MenuDialog.Builder(this)
                .setCancel("取消") // 设置 null 表示不显示取消按钮
                //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
                .setList(datalist)
                .setListener(new MenuDialog.OnListener() {

                    @Override
                    public void onSelected(Dialog dialog, int position, String text) {
                        switch (flage) {
                            case 1:
                                if (!sbCitypartnerStoreType.getRightText().equals(text)) {
                                    sbCitypartnerStoreType.setRightText(text);
                                }
                                TypeId = mMerchantTypeLists.get(position).getId();
                                break;
                            case 2:
                                if (!sbCitypartnerStoreBusiness.getRightText().equals(text)) {
                                    typeid = mMerchantChildTypeLists.get(position).getId();
                                    sbCitypartnerStoreBusiness.setRightText(text);
                                    depositLow = mMerchantChildTypeLists.get(position).getHirePurchaseLow();
                                    depositHigh = mMerchantChildTypeLists.get(position).getHirePurchaseHigh();

                                    baozhengjinText.setHint(depositLow + "元 - " + depositHigh + "元 ");


                                    manageRateLow = mMerchantChildTypeLists.get(position).getManageRateLow();
                                    manageRateHigh = mMerchantChildTypeLists.get(position).getManageRateHigh();

                                    guanlifeiText.setHint(manageRateLow + "% - " + manageRateHigh + "%");

                                }
                                break;
                        }

                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        toast("取消了");
                    }
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
                .show();
    }


    /**
     * 选择城市
     *
     * @param mSelectProviceEnities
     */
    private void getSelectCity(ArrayList<SelectCityEnity> mSelectProviceEnities) {
        SelectCityPicker selectCityPicker = new SelectCityPicker(getActivity(), mSelectProviceEnities);
        selectCityPicker.setHalfScreen(true);
        selectCityPicker.setCancelTextColor(R.color.colorAccent);
        selectCityPicker.setSubmitTextColor(R.color.colorAccent);
        selectCityPicker.setOnCitySelectListener(new SelectCityPicker.OnCitySelectListener() {
            @Override
            public void onCitySelect(String province, String city, String county, String town, int provinceId, int cityId, int countyId, int townId) {
                if (!province.equals("") && city.equals("") && county.equals("") && town.equals("")) {
                    sbCitypartnerStoreCity.setRightText(province);
                } else if (!province.equals("") && !city.equals("") && !county.equals("") && town.equals("")) {
                    sbCitypartnerStoreCity.setRightText(province);
                } else if (!province.equals("") && !city.equals("") && county.equals("") && town.equals("")) {
                    sbCitypartnerStoreCity.setRightText(province + "-" + city);
                } else {
                    sbCitypartnerStoreCity.setRightText(province + "-" + city + "-" + county + "-" + town);
                }
                proviceId = provinceId;
                mcityId = cityId;
                districCountryId = countyId;
                streetId = townId;
            }
        });
        selectCityPicker.show();
    }

    /**
     * 时间选择器
     */
    private void checktimer(final int flage) {
        String[] arreaArr = getActivity().getResources().getStringArray(R.array.timer_array);
        final List<String> shooting_type_list = Arrays.asList(arreaArr);
        new MenuDialog.Builder(this)
                .setCancel("取消") // 设置 null 表示不显示取消按钮
                //.setAutoDismiss(false) // 设置点击按钮后不关闭对话框
                .setList(shooting_type_list)
                .setListener(new MenuDialog.OnListener() {

                    @Override
                    public void onSelected(Dialog dialog, int position, String text) {
                        switch (flage) {
                            case 1:
                                sb_starttimer_store_product.setRightText(shooting_type_list.get(position).toString());
                                break;
                            case 2:
                                sb_endtimer_store_product.setRightText(shooting_type_list.get(position).toString());
                                break;
                        }
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        toast("取消了");
                    }
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
                .show();
    }


}

